package com.tsd.jxc.stock.controller;

import com.alibaba.fastjson.JSON;
import com.tsd.common.controller.BaseController;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.vo.ErrorCode;
import com.tsd.core.vo.HttpResult;
import com.tsd.core.vo.PageBean;
import com.tsd.jxc.stock.entity.JxcStockInventoryItem;
import com.tsd.jxc.stock.entity.JxcStockInventoryItemExt;
import com.tsd.jxc.stock.service.JxcStockInventoryItemService;
import com.tsd.system.entity.SysUser;
import com.tsd.system.entity.SysUserExt;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：JxcStockInventoryItem盘点商品清单 控制层
 *
 * @author Hillpool
 * @date 2022/07/29 15:06:33
 */
@Controller
@Scope("prototype")
@RequestMapping("/api/v2")
@Api(tags = "进销存-盘点商品清单", description = "盘点商品清单的API描述")
public class JxcStockInventoryItemController extends BaseController {

    @Resource
    private JxcStockInventoryItemService jxcStockInventoryItemService;

    @ApiOperation(value = "根据sid获取盘点商品清单", notes = "根据sid获取盘点商品清单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sid", value = "盘点商品清单sid", required = true, dataType = "string", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/getJxcStockInventoryItemBySid.action", method = RequestMethod.POST)
    public HttpResult<JxcStockInventoryItem> getJxcStockInventoryItemBySid() {
        HttpResult<JxcStockInventoryItem> hr = new HttpResult<JxcStockInventoryItem>();
        try {
            Map<String, Object> map = super.getParams(request);
            String sid = HlpUtils.getString(map, "sid");
            super.checkEmpty(sid, "请提供参数sid");
            JxcStockInventoryItem jxcStockInventoryItem = jxcStockInventoryItemService.queryBySid(sid);
            hr.setData(jxcStockInventoryItem);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "根据id删除盘点商品清单", notes = "根据id删除盘点商品清单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sids", value = "盘点商品清单sids", required = true, dataType = "string", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/deleteJxcStockInventoryItems.action", method = RequestMethod.POST)
    public HttpResult<String> deleteJxcStockInventoryItems() {
        HttpResult<String> hr = new HttpResult<>();
        try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            Object sids = map.get("sids");
            super.checkEmpty(sids, "请提供参数sids");
            List<String> list = JSON.parseArray(JSON.toJSONString(sids), String.class);
            jxcStockInventoryItemService.deleteJxcStockInventoryItems(list, opUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }


    @ApiOperation(value = "保存盘点商品清单", notes = "保存盘点商品清单，具有id值则更新，没有id值则新增")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "obj", value = "盘点商品清单", required = true, paramType = "body", dataType = "JxcStockInventoryItem")
    })
    @ResponseBody
    @RequestMapping(value = "/saveJxcStockInventoryItem.action", method = RequestMethod.POST)
    public HttpResult<Object> saveJxcStockInventoryItem() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            String json = JSON.toJSONString(map);
            JxcStockInventoryItem t = JSON.parseObject(json, JxcStockInventoryItem.class);
            jxcStockInventoryItemService.saveJxcStockInventoryItem(t, opUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "批量保存盘点商品清单", notes = "批量保存盘点商品清单，具有id值则更新，没有id值则新增")
    @ResponseBody
    @RequestMapping(value = "/batchSaveJxcStockInventoryItem.action", method = RequestMethod.POST)
    public HttpResult<Object> batchSaveJxcStockInventoryItem() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            SysUserExt opUser = super.getSysUser();
            List<JxcStockInventoryItemExt> list = super.getParamsList("list", JxcStockInventoryItemExt.class, true);
            jxcStockInventoryItemService.batchSaveJxcStockInventoryItem(list, opUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "分页查询盘点商品清单", notes = "分页查询盘点商品清单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "名称", required = false, dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "pageNo", value = "页码", required = false, dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "pageSize", value = "页大小，每页记录数", required = false, dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "sorts", value = "排序设置，方式['property1 desc','property2']", required = false, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "totalRows", value = "总记录数，为了减少统计总记录数，一般从第二页开始前端提供totalRows", required = false, dataType = "int", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/findJxcStockInventoryItems.action", method = RequestMethod.POST)
    public HttpResult<Object> findJxcStockInventoryItems() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            Map<String, Object> map = super.getParams(request);
            Map<String, Object> params = new HashMap<>(8);
            HlpUtils.copyIntegerValue(map, params, "dr", "dr", false, 0);
            HlpUtils.copyStringValue(map, params, "search_value", "search_value", false, true);
            PageBean page = new PageBean(map);
            List<JxcStockInventoryItemExt> list = jxcStockInventoryItemService.findJxcStockInventoryItems(params, page);
            hr.setData(list);
            hr.setPage(page);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "根据盘点单sid分页获取盘点商品清单", notes = "根据盘点单sid分页获取盘点商品清单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "名称", required = false, dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "pageNo", value = "页码", required = false, dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "pageSize", value = "页大小，每页记录数", required = false, dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "sorts", value = "排序设置，方式['property1 desc','property2']", required = false, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "totalRows", value = "总记录数，为了减少统计总记录数，一般从第二页开始前端提供totalRows", required = false, dataType = "int", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/selectGoodsByInventoryId.action", method = RequestMethod.POST)
    public HttpResult<Object> selectGoodsByInventoryId() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            Map<String, Object> map = super.getParams(request);
            Map<String, java.lang.Object> params = new HashMap<>(8);
            HlpUtils.copyIntegerValue(map, params, "dr", "dr", false, 0);
            HlpUtils.copyIntegerValue(map, params, "state", "state", false, null);
            HlpUtils.copyStringValue(map, params, "code", "code", false, false);
            HlpUtils.copyStringValue(map, params, "inventory_sid", "inventory_sid", false, false);
            HlpUtils.copyIntegerValue(map, params, "surplus", "surplus", false, 0);
            PageBean page = new PageBean(map);
            List<JxcStockInventoryItemExt> goodsId = jxcStockInventoryItemService.queryGoodsByInventoryIdByPage(params, page);
            hr.setData(goodsId);
            hr.setPage(page);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "根据导入数据盘点商品清单", notes = "盘点商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "obj", value = "盘点商品清单", required = true, paramType = "body", dataType = "JxcStockInventoryItem")
    })
    @ResponseBody
    @RequestMapping(value = "/checkJxcStockInventoryItem.action", method = RequestMethod.POST)
    public HttpResult<Object> checkJxcStockInventoryItem() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            Object data = map.get("data");
            List<Map> list = JSON.parseArray(JSON.toJSONString(data), Map.class);
            jxcStockInventoryItemService.checkJxcStockInventoryItem(list, opUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "根据盘盈盘亏筛选商品清单", notes = "筛选商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "obj", value = "盘盈|盘亏", required = true, paramType = "body", dataType = "Array")
    })
    @ResponseBody
    @RequestMapping(value = "/selectJxcStockInventoryItemBySurplus.action", method = RequestMethod.POST)
    public HttpResult<Object> selectJxcStockInventoryItemBySurplus() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            Map<String, Object> map = super.getParams(request);
            Object data = map.get("data");
            String sid = map.get("sid").toString();
            List<String> list = JSON.parseArray(JSON.toJSONString(data), String.class);
            PageBean page = new PageBean(map);
            List<JxcStockInventoryItemExt> result = jxcStockInventoryItemService.selectGoodsBySurplus(sid, list, page);
            hr.setData(result);
            hr.setPage(page);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }
}
